var express = require('express');
var router = express.Router();
const studentService = require("../src/services/studentServices")
/* GET users listing. */
//显示学生列表
router.get('/show', async function (req, resp) {
  let res = await studentService.AccordingToStudents();
  resp.json({
    "status": 0,
    "message": "success",
    "total": 180,
    "datas": res
  })
});
//新增学生
router.post('/add', async function (req, resp) {
  let {
    username,
    age,
    gender,
    score,
    address
  } = req.body
  let res = await studentService.Addstudents(username, age, gender, score, address);
  if (res != null) {
    resp.json({
      'code': 1,
      'msg': '添加成功'
    })
  } else {
    resp.json({
      'code': 0,
      'msg': '添加失败'
    })
  }
});
//删除学生
router.delete('/del/:id', async function (req, resp) {
  let {
    id
  } = req.params
  let res = await studentService.Delstudents(id);
  if (res.deletedCount == 1) {
    resp.json({
      'code': 1,
      'msg': '删除成功'
    })
  } else {
    resp.json({
      'code': 0,
      'msg': '删除失败'
    })
  }
});
//查询学生
router.get('/studentDetail/:id', async function (req, resp) {
  let {
    id
  } = req.params;
  let res = await studentService.detailStudents(id);
  console.log(res)
  if (res != null) {
    resp.json({
      'code': 1,
      'msg': '查询成功',
      'data': res
    })
  } else {
    resp.json({
      'code': 0,
      'msg': '查询失败'
    })
  }
});
//修改学生
router.put('/updatastudent', async function (req, resp) {
  let {
    id,
    name,
    age,
    gender,
    score,
    address
  } = req.body;
  let res = await studentService.updataStudents({
    '_id': id
  }, {
    name,
    age,
    gender,
    score,
    address
  });
  if (res.modifiedCount) {
    resp.json({
      'code': 1,
      'msg': '修改成功'
    })
  }
});
//分页
router.get('/page', async function (req, resp) {
  let {
    currentpage,
    pagesize
  } = req.query;

  console.log(currentpage, pagesize);

  let pageBean = await studentService.getAccessToPag({
    "currentpage": parseInt(currentpage),
    "pagesize": parseInt(pagesize)
  })

  resp.json({
    "status": 0,
    "message": "success",
    ...pageBean
  })
});

module.exports = router;